package com.example.databasehomework;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ChooseRoomListAdapter extends RecyclerView.Adapter {

    public Context context;
    public List<ChooseRoomListItemBean> data;
    public OnItemClickListener itemClickListener;
    public OnItemLongClickListener itemLongClickListener;

    public ChooseRoomListAdapter(Context context) {
        this.context = context;
    }

    public ChooseRoomListAdapter(Context context, List<ChooseRoomListItemBean> data){
        this.context=context;
        this.data=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.chooseroom_list_item, parent, false);
        return new MyViewHolder(view,itemClickListener, itemLongClickListener);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView id =((MyViewHolder)holder).v.findViewById(R.id.chooseroom_list_item_id);
        id.setText(data.get(position).roomId);
        CheckBox cb = ((MyViewHolder)holder).v.findViewById(R.id.chooseroom_list_item_check);
        if (data.get(position).ifChecked) {
            cb.setChecked(true);
        } else {
            cb.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

        private OnItemClickListener onItemClickListener;
        private OnItemLongClickListener onItemLongClickListener;
        private View v;

        public MyViewHolder(View itemView,OnItemClickListener onItemClickListener, OnItemLongClickListener onItemLongClickListener) {
            super(itemView);
            this.onItemClickListener=onItemClickListener;
            this.onItemLongClickListener = onItemLongClickListener;
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            v=itemView;
        }

        @Override
        public void onClick(View view) {
            if(onItemClickListener!=null)
                onItemClickListener.onItemClick(v,getAdapterPosition());
        }

        @Override
        public boolean onLongClick(View view) {
            if(onItemLongClickListener!=null)
                onItemLongClickListener.onItemLongClick(v,getAdapterPosition());
            return false;
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.itemClickListener=onItemClickListener;
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view,int position);
    }

    public void setOnLongClickListener (OnItemLongClickListener OnItemLongClickListener) {
        this.itemLongClickListener = OnItemLongClickListener;
    }
}